package io.renren.modules.yst.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.utils.Constant;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.app.form.CheckOutForm;
import io.renren.modules.yst.entity.CompanyEntity;
import io.renren.modules.yst.entity.WorkerCardRecordEntity;
import io.renren.modules.yst.form.WorkerLoginForm;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.yst.entity.WorkerCardEntity;
import io.renren.modules.yst.form.WorkerReplaceCarForm;
import io.renren.modules.yst.service.CompanyService;
import io.renren.modules.yst.service.WorkerCardRecordService;
import io.renren.modules.yst.service.WorkerCardService;
import io.renren.modules.yst.vo.CheckRecordVo;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.yst.entity.WorkerEntity;
import io.renren.modules.yst.service.WorkerService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;


/**
 * 司机
 *
 * @author lazyrm
 * @email lazyrm0720@gmail.com
 * @date 2022-01-07 21:01:23
 */
@RestController
@RequestMapping("yst/worker")
public class WorkerController extends AbstractController {
    @Autowired
    private WorkerService workerService;

    @Autowired
    private WorkerCardService workerCardService;

    @Autowired
    private WorkerCardRecordService workerCardRecordService;

    @Autowired
    private CompanyService companyService;

    @PostMapping("/register")
    @ApiOperation("司机登记")
    @Transactional
    public R register(@RequestBody WorkerLoginForm form) {
        //表单校验
        ValidatorUtils.validateEntity(form);
        WorkerEntity worker = workerService.getById(getWorkerId());
        WorkerCardEntity workerCard = new WorkerCardEntity();
        if (StringUtils.isEmpty(worker.getUsername())) {

            worker.setUsername(form.getUsername());
            worker.setBankname(form.getBankName());
            worker.setBankcardnumber(form.getBankCardNumber());
            worker.setIdcardnumber(form.getIdCardNumber());
            worker.setIdcardnumberpic(form.getIdCardNumberPic());
            workerService.saveOrUpdate(worker);


            workerCard.setWorkerId(worker.getWorkerId());
            workerCard.setCarNumber(form.getCarNumber());
            workerCard.setCarNumberPic(form.getCarNumberPic());
            workerCard.setCreateTime(new Date());

            workerCardService.saveOrUpdate(workerCard);

        }

        return R.ok().put("worker", worker).put("workercard", workerCard);
    }


    @PostMapping("/replacecar")
    @ApiOperation("司机登记")
    @Transactional
    public R register(@RequestBody WorkerReplaceCarForm form) {
        //表单校验
        ValidatorUtils.validateEntity(form);
        WorkerEntity worker = workerService.getById(getWorkerId());


        WorkerCardEntity workerCard = new WorkerCardEntity();
        workerCard.setWorkerId(worker.getWorkerId());
        workerCard.setCarNumber(form.getCarNumber());
        workerCard.setCarNumberPic(form.getCarNumberPic());
        workerCard.setCreateTime(new Date());

        workerCardService.saveOrUpdate(workerCard);


        return R.ok().put("workerCard", workerCard);
    }


    /**
     *
     */
    @PostMapping("checkout")
    @ApiOperation("出关更新")
    public R checkOut(@RequestBody CheckOutForm form) {
        WorkerCardRecordEntity workerCardRecord = workerCardRecordService.getById(form.getWorkerCardRecordId());
        WorkerCardEntity workerCard = workerCardService.getById(workerCardRecord.getCardId());
        if (workerCard.getWorkerId() == getWorkerId()) {
            workerCardRecord.setUpdateTime(new Date());
            workerCardRecord.setFreight(form.getFreight());
            workerCardRecord.setCompanyId(form.getCompanyId());
            workerCardRecord.setSerialNumber(form.getSerialnumber());
            workerCardRecordService.saveOrUpdate(workerCardRecord);
            return R.ok();
        } else {
            return R.error();
        }
    }


    @GetMapping("getcompany")
    @ApiOperation("获取热力公司列表")
    public R getcompany() {
//        List conpanys= companyProperties.getInfos();
        List<Map<String, Object>> conpanys = companyService.listMaps(new QueryWrapper<CompanyEntity>().select("company_id", "company_name"));
        return R.ok().put("companys",conpanys);
    }


    @GetMapping("getlastcard")
    @ApiOperation("获取最近的登记卡")
    public R getlastcard() {
        WorkerCardEntity workerCardEntity =workerCardService.getlastOne(getWorkerId());

        return R.ok().put("workerCard",workerCardEntity);
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("yst:worker:list")
    public R list(@RequestParam Map<String, Object> params) {


        PageUtils page = workerService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 列表
     */
    @RequestMapping("/listrecord")
    public R listrecord(@RequestParam Map<String, Object> params) {
//        workerCardService.getMap()

        List list=workerCardService.getWorkerCardIDs(getWorkerId());


        params.put("card_ids", list);
//
//        PageUtils page = workerCardRecordService.queryPage(params);
        long curPage = 1;
        long limit = 10;

        if(params.get(Constant.PAGE) != null){
            curPage = Long.parseLong((String)params.get(Constant.PAGE));
        }
        if(params.get(Constant.LIMIT) != null){
            limit = Long.parseLong((String)params.get(Constant.LIMIT));
        }


        QueryWrapper<CheckRecordVo> queryWrapper = new QueryWrapper<CheckRecordVo>();
        Page<CheckRecordVo> page= new Page<CheckRecordVo>(  curPage,limit);
        //自定义getAll接口
        PageUtils result = workerCardRecordService.queryPage(page, queryWrapper.in("t1.card_id",list));



        return R.ok().put("page", result);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{workerId}")
    @RequiresPermissions("yst:worker:info")
    public R info(@PathVariable("workerId") Long workerId) {
        WorkerEntity worker = workerService.getById(workerId);

        return R.ok().put("worker", worker);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("yst:worker:save")
    public R save(@RequestBody WorkerEntity worker) {
        workerService.save(worker);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("yst:worker:update")
    public R update(@RequestBody WorkerEntity worker) {
        workerService.updateById(worker);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("yst:worker:delete")
    public R delete(@RequestBody Long[] workerIds) {
        workerService.removeByIds(Arrays.asList(workerIds));

        return R.ok();
    }

}
